﻿// ------------------------------------------------------------------------------
// <copyright file="TapEffect.cs" company="skumaronline.com">
//    Copyright (C) 2012 Suresh Kumar T.
//
//    This program is free software; you can redistribute it and/or
//    modify it under the terms of the GNU General Public License
//    as published by the Free Software Foundation version 2
//    of the License.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    http://www.gnu.org/licenses/gpl-2.0.html
//
// </copyright>
// ------------------------------------------------------------------------------

namespace StoreAppLib.Animations
{
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Media.Animation;

    /// <summary>
    /// Dependency object to provide tap effect to XAML controls
    /// </summary>
    public class TapEffect : DependencyObject
    {
        /// <summary>
        /// Enabled dependency property
        /// </summary>
        public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached(
          "Enabled",
          typeof(bool),
          typeof(TapEffect),
          new PropertyMetadata(false, OnEnabledPropertyChanged));

        /// <summary>
        /// Enabled Get property
        /// </summary>
        /// <param name="source">The source</param>
        /// <returns>Returns enabled property </returns>
        public static bool GetEnabled(DependencyObject source)
        {
            return (bool)source.GetValue(EnabledProperty);
        }

        /// <summary>
        /// Enabled Set property
        /// </summary>
        /// <param name="source">The source</param>
        /// <param name="value">Sets enabled property</param>
        public static void SetEnabled(DependencyObject source, bool value)
        {
            source.SetValue(EnabledProperty, value);
        }

        /// <summary>
        /// Call back event to capture Enabled property change
        /// Attach/detach events
        /// </summary>
        /// <param name="target">The target</param>
        /// <param name="args">Property changed call back event argument</param>
        private static void OnEnabledPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
        {
            FrameworkElement element = target as FrameworkElement;

            if (element != null)
            {
                if ((bool)args.NewValue == true)
                {
                    element.PointerPressed += OnPointerPressed;
                    element.PointerReleased += OnPointerReleased;
                }
                else
                {
                    element.PointerPressed -= OnPointerPressed;
                    element.PointerReleased -= OnPointerReleased;
                }
            }
        }

        /// <summary>
        /// Occurs when the pointer device that previously initiated a Press action is released
        /// </summary>
        /// <param name="sender">The framework element</param>
        /// <param name="e">Pointer routed event argument</param>
        private static void OnPointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;

            if (element != null)
            {
                // If have transforms
                if (element.Projection != null || (element.RenderTransform != null && element.RenderTransform.GetType() != typeof(MatrixTransform)))
                {
                    return;
                }

                //// Start pointer up animation
                Storyboard storyboard = new Storyboard();

                PointerUpThemeAnimation animation = new PointerUpThemeAnimation();

                storyboard.Children.Add(animation);

                Storyboard.SetTarget(animation, element);

                storyboard.Begin();

                //// Release the pointer captured during down event
                element.ReleasePointerCapture(e.Pointer);
            }
        }

        /// <summary>
        /// Occurs when the pointer device initiates a Press action within this element
        /// </summary>
        /// <param name="sender">The framework element</param>
        /// <param name="e">Pointer routed event argument</param>
        private static void OnPointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;

            if (element != null)
            {
                // Prevents interference with any existing transforms
                if (element.Projection != null || (element.RenderTransform != null && element.RenderTransform.GetType() != typeof(MatrixTransform)))
                {
                    return;
                }

                //// Capture the pointer so that release is fired for the element
                element.CapturePointer(e.Pointer);

                //// Starts pointer down animation
                Storyboard storyboard = new Storyboard();

                PointerDownThemeAnimation animation = new PointerDownThemeAnimation();

                storyboard.Children.Add(animation);

                Storyboard.SetTarget(animation, element);

                storyboard.Begin();
            }
        }
    }
}
